package com.xbongbong.pro.rabbitmq.producer.impl;/*/*/

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.ConsistencySyncRabbitMqDTO;
import com.xbongbong.pro.rabbitmq.binders.ConsistencySyncProducerBinder;
import com.xbongbong.pro.rabbitmq.producer.ConsistencySyncProducer;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 数据一致性 消息 生产者
 * @author 魏荣杰
 * @date 2019/6/11 10:09
 * @since v1.0
 * @version v1.0
 */
@Service("consistencySyncProducer")
@EnableBinding(ConsistencySyncProducerBinder.class)
public class ConsistencySyncProducerImpl implements ConsistencySyncProducer {
    private static Logger logger = LoggerFactory.getLogger(ConsistencySyncProducerImpl.class);

    @Resource
    private ConsistencySyncProducerBinder consistencySyncProducerBinder;

    @Override
    public void syncData(ConsistencySyncRabbitMqDTO consistencySyncRabbitMqDTO, XbbRefTypeEnum xbbRefTypeEnum) throws XbbException {
        MessageBuilder messageBuilder;
        if (Objects.nonNull(xbbRefTypeEnum) && Objects.nonNull(consistencySyncRabbitMqDTO)) {
            messageBuilder = MessageBuilder.withPayload(consistencySyncRabbitMqDTO);
            switch (xbbRefTypeEnum) {
                case CUSTOMER:
                    consistencySyncProducerBinder.syncCustomerOutput().send(messageBuilder.build());
                    logger.info("ConsistencySyncProducerImpl send success");
                    break;
                case CONTACT:
                    consistencySyncProducerBinder.syncContactOutput().send(messageBuilder.build());
                    break;
                case CONTRACT:
                    consistencySyncProducerBinder.syncContractOutput().send(messageBuilder.build());
                    break;
                case SALES_OPPORTUNITY:
                    consistencySyncProducerBinder.syncOpportunityOutput().send(messageBuilder.build());
                    break;
                case PRODUCT:
                    consistencySyncProducerBinder.syncProductOutput().send(messageBuilder.build());
                    break;
                case USER:
                    consistencySyncProducerBinder.syncUserOutput().send(messageBuilder.build());
                    break;
                case SUPPLIER:
                    consistencySyncProducerBinder.syncSupplierOutput().send(messageBuilder.build());
                    break;
                case WAREHOUSE:
                    consistencySyncProducerBinder.syncWarehouseOutput().send(messageBuilder.build());
                    break;
                case QUOTATION:
                    consistencySyncProducerBinder.syncQuotationOutput().send(messageBuilder.build());
                    break;
                case PURCHASE:
                    consistencySyncProducerBinder.syncPurchaseOutput().send(messageBuilder.build());
                    break;
                case BATCH_FLOW_BILL:
                    consistencySyncProducerBinder.syncBatchFlowBillOutput().send(messageBuilder.build());
                    break;
                case SEQ_SEARCH:
                    consistencySyncProducerBinder.syncSeqSearchOutput().send(messageBuilder.build());
                    break;
                case PAYMENT:
                    consistencySyncProducerBinder.syncPaymentOutput().send(messageBuilder.build());
                    break;
                case PAY_PLAN:
                    consistencySyncProducerBinder.syncPayPlanOutput().send(messageBuilder.build());
                    break;
                case CLUE:
                    consistencySyncProducerBinder.syncClueOutput().send(messageBuilder.build());
                    break;
                case MARKET_ACTIVITY:
                    consistencySyncProducerBinder.syncMarketActivityOutput().send(messageBuilder.build());
                    break;
                case COMPETITOR:
                    consistencySyncProducerBinder.syncCompetitorOutput().send(messageBuilder.build());
                    break;
                case INVOICE:
                    consistencySyncProducerBinder.syncInvoiceOutput().send(messageBuilder.build());
                    break;
                case CONTRACT_OUTSTOCK:
                    consistencySyncProducerBinder.syncContractOutStockOutput().send(messageBuilder.build());
                    break;
                default:
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100069.getCode(),  String.format(SystemErrorCodeEnum.API_ERROR_100069.getMsg(), xbbRefTypeEnum.getName()));
            }
        }
    }
}
